package org.zffc.common.util;


import org.zffc.common.expection.ZffcBootException;

/**
 * 断言工具类
 */
public class AssertUtils {

    /**
     * 确保对象为空,如果不为空抛出异常
     *
     * @param msg
     * @param obj
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertEmpty(String msg, Object obj) {
        if (CommonUtil.isObjectNotEmpty(obj)) {
            throw new ZffcBootException(msg);
        }
    }


    /**
     * 确保对象不为空,如果为空抛出异常
     *
     * @param msg
     * @param obj
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertNotEmpty(String msg, Object obj) {
        if (CommonUtil.isObjectEmpty(obj)) {
            throw new ZffcBootException(msg);
        }
    }


    /**
     * 验证对象是否相同
     *
     * @param message
     * @param expected
     * @param actual
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertEquals(String message, Object expected,
                                    Object actual) {
        if (CommonUtil.isEqual(expected, actual)) {
            return;
        }
        throw new ZffcBootException(message);
    }

    /**
     * 验证不相同
     *
     * @param message
     * @param expected
     * @param actual
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertNotEquals(String message, Object expected,
                                       Object actual) {
        if (CommonUtil.isEqual(expected, actual)) {
            throw new ZffcBootException(message);
        }

    }

    /**
     * 验证是否相等
     *
     * @param message
     * @param expected
     * @param actual
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertSame(String message, Object expected,
                                  Object actual) {
        if (expected == actual) {
            return;
        }
        throw new ZffcBootException(message);
    }

    /**
     * 验证不相等
     *
     * @param message
     * @param unexpected
     * @param actual
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertNotSame(String message, Object unexpected,
                                     Object actual) {
        if (unexpected == actual) {
            throw new ZffcBootException(message);
        }
    }

    /**
     * 验证是否为真
     *
     * @param message
     * @param condition
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertTrue(String message, boolean condition) {
        if (!condition) {
            throw new ZffcBootException(message);
        }
    }

    /**
     * 验证 condition是否为false
     *
     * @param message
     * @param condition
     * @throws  org.zffc.common.expection.ZffcBootException
     * @author zhangfei
     */
    public static void assertFalse(String message, boolean condition) {
        assertTrue(message, !condition);
    }

}
